# -*- coding:utf-8 -*-

'''第11章'''

f=open('somefile.txt','w')
f.write('hello world\n')
f.write('tzj\n')
f.close()

f=open('somefile.txt','a')
f.write('append something')
f.close()

f=open('somefile.txt','r')
print f.read()

with open('somefile.txt') as f:
    while True:
        #读取一个字符，下次再调用read，会从下个字符开始读
        char=f.read(1)
        if not char:
            break
        print char

with open('somefile.txt') as f:
    while True:
        #读取一行，下次再调用readline，会从下一行开始读
        line=f.readline()
        if not line:#没有下一行了，退出循环
            break
        print line
        
print '------------fileinput-----------'
import fileinput
for line in fileinput.input('somefile.txt'):
    print line